<?php

namespace Admin\Controller;
use Think\Controller;

class OrderController extends BaseController{
	
	public function array_sort($array,$key){
		if(is_array($array)){
			$key_array = null;
			$new_array = null;
			for($i=0;$i<count($array);$i++){
				$key_array[$array[$i][$key]] = $i;
			}
			krsort($key_array);
			$j=0;
			foreach($key_array as $k=>$v){
				$new_array[$j] = $array[$v];
				$j++;
			}
			unset($key_array);
			return $new_array;
		}else{
			return $array;
		}
	}


	
	public function index(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		$nickname = I('nickname');
		$starttime =str_replace("+"," ",I('starttime'));
		$endtime = str_replace("+"," ",I('endtime'));
		$game = I('game');
		$is_add = I('is_add');

		if($nickname){
			if (is_numeric($nickname)) {
				$map['username'] = array('like',"%$nickname%");
			} else{
				$map['nickname'] = array('like',"%$nickname%");
			}
		}

		if ($game) {
			$map['game'] = $game;
		}

		if ($is_add === '0' || $is_add) {
			$map['is_add'] = $is_add;
			$map['state'] = 1;
		}

		if($starttime && $endtime){
			$start = strtotime($starttime);
			$end = strtotime($endtime);
			$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		}
		$map['is_under'] = 1;
		$order = M('order');
		$count = $order->where($map)->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list1 = array();
		$list1 = $order->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();

		for($i=0;$i<count($list1);$i++){
			if($list1[$i]['userid']){
				$list1[$i]['user'] = M('user')->where("id = {$list1[$i]['userid']}")->find();
			}else{
				$list1[$i]['user'] = M('user')->where("id = {$list1[$i]['uid']}")->find();
			}
		}
		
		$this->assign('list',$list1);
		$this->assign('show',$show);
		$this->assign('nickname',$nickname);
		$this->assign('game',$game);
		$this->assign('is_add',$is_add);
		$this->assign('starttime',$starttime);
		$this->assign('endtime',$endtime);
		$this->display();
	}

/**
     * 修改用户下注
     */
	public function editpost(){
        if($_POST){
            $id=$_POST['id'];
            //老记录
            $old = $_POST['old'];
            $number=$_POST['newnumber'];
            //读取id的数据
            $data=M('order')->where('id='.$id)->find();
            if($data){
                //修改字段tzcode
                $new=array(
                    'jincai'=>$number
                );
                $res=M('order')->where('id='.$id)->setField($new);
                /**
                 * 修改message表记录
                 * 根据order表关联的用户id查询出message表的数据
                 * 然后在根据游戏类型查询对应的数据
                 * 最后根据old内容查询相同的数据并且进行修改
                 * 根据时间
                 */
                if($res){
                    $time=date("H:i:s",$data['time']);
                    $where=array(
                        'uid'=>$data['userid'],
                        'game'=>$data['game'],
                        'content'=>$old,
                        'time'=>$time
                    );
                    $info=M('message')->where($where)->select();
                    if($info){
                        $messnew=array(
                            'content'=>$number
                        );
                        $stat=M('message')->where($where)->setField($messnew);
                        if($stat){
                            /**
                             * 修改系统通知
                             */
                            $wheres=array(
                                'uid'=>$data['userid'],
                                'game'=>$data['game'],
                                'type'=>'error',
                                'head_img_url'=>'/Public/main/img/kefu.jpg',
                                'time'=>$time
                            );
                            $infos=M('message')->where($wheres)->select();
                            if($infos){
                                $xitong=array(
                                    'content'=>'「'.$number.'」,竞猜成功'
                                );
                                $xitonginfo=M('message')->where($wheres)->setField($xitong);
                                if($xitonginfo){
                                    $this->success('修改成功');
                                }
                            }
                        }else{
                            $this->error('修改失败');
                        }
                    }
                }else{
                    $this->error('修改失败');
                }
            }
        }
    }
	
	
	public function bztj_bak(){
	

		$nickname = I('nickname');
		$starttime =str_replace("+"," ",I('starttime'));
		$endtime = str_replace("+"," ",I('endtime'));
		$game = I('game');
		$is_add = I('is_add');

		if($nickname){
			if (is_numeric($nickname)) {
				$map['username'] = array('like',"%$nickname%");
			} else{
				$map['nickname'] = array('like',"%$nickname%");
			}
		}

		if ($game) {
			$map['game'] = $game;
		}

		if ($is_add === '0' || $is_add) {
			$map['is_add'] = $is_add;
			$map['state'] = 1;
		}

		if($starttime && $endtime){
			$start = strtotime($starttime);
			$end = strtotime($endtime);
			$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		}
		$map['is_under'] = 1;
		$order = M('order_bz');
		$count = $order->where($map)->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list1 = array();
		$list1 = $order->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();

		//统计
		$tongji = $order->where($map)->field("sum(add_points) as sum_add,sum(del_points) as sum_del")->find();

		for($i=0;$i<count($list1);$i++){
			if($list1[$i]['userid']){
				$list1[$i]['user'] = M('user')->where("id = {$list1[$i]['userid']}")->find();
			}else{
				$list1[$i]['user'] = M('user')->where("id = {$list1[$i]['uid']}")->find();
			}
		}
		
		$this->assign('list',$list1);
		$this->assign('tongji',$tongji);
		$this->assign('show',$show);
		$this->assign('nickname',$nickname);
		$this->assign('game',$game);
		$this->assign('is_add',$is_add);
		$this->assign('starttime',$starttime);
		$this->assign('endtime',$endtime);
		$this->display();
	}


	public function export(){
		$map = array();
		$map['is_add'] = 0;
		$map['state'] = 1;
		$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		$list = M('order')->where($map)->select();
		$table = '';
        $table .= "<table>
            <thead>
                <tr>
                    <th class='name'>类型</th>
                     <th class='name'>期号</th>
                    <th class='name'>下注时间</th>
                    <th class='name'>昵称</th>
                    <th class='name'>用户号</th>
                    <th class='name'>竞猜</th>
                    <th class='name'>进项</th>
                    <th class='name'>出项</th>
                    <th class='name'>输赢</th>
                    <th class='name'>余额</th>
                </tr>
            </thead>
            <tbody>";
        foreach ($list as $v) {
        	$ying = $v['add_points'] - $v['del_points'];
            $table .= "<tr>
                    <td class='name'>{$v['game']}</td>
                    <td class='name'>{$v['number']}</td>
                    <td class='name'>".date('Y-m-d H:i:s',$v['time'])."</td>
                    <td class='name'>{$v['nickname']}</td>
                    <td class='name'>{$v['username']}</td>
                    <td class='name'>{$v['jincai']}</td>
                    <td class='name'>{$v['add_points']}</td>
                    <td class='name'>{$v['del_points']}</td>
                    <td class='name'>{$ying}</td>
                    <td class='name'>{$balacne}</td>
                </tr>";
        }
        $table .= "</tbody>
        </table>";
//通过header头控制输出excel表格
            header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");  
        header("Content-Type:application/vnd.ms-execl");  
        header("Content-Type:application/octet-stream");  
        header("Content-Type:application/download");;  
        header('Content-Disposition:attachment;filename="'.date('Y-m-d H:i:s',time()).'未结算订单.xls"');  
        header("Content-Transfer-Encoding:binary");  
        echo $table;
    
	}

	public function del(){
		$id = I('id');
		if($id){
			$order = M('order')->where("id={$id}")->find();
			$res = M('order')->where("id = $id")->delete();
			M('user')->where("id = {$order['t_id']}")->setDec('yong',$order['del_points']*C('fenxiao')*0.01);
			M('user')->where("id = {$order['t_id']}")->setDec('t_add', $order['del_points']*C('fenxiao')*0.01);
			M('push_money')->where("orderid={$id}")->delete();
			
			if($res){
				$this->success('删除成功！');
			}else{
				$this->error('删除失败！');
			}
		}
	}

	public function del_order(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				$endtime = I('endtime');
				$safeCode = I('safeCode');

				if ($safeCode != C('safeCode')) {
					$this->error('安全码错误');
				}

				if (!$endtime) {
					$this->error("请选择时间");
				}
				$endtimestr = $endtime . " 23:59:59";
				$endstr = strtotime($endtime . " 23:59:59");

				M('order')->where("time <= $endstr")->delete();
				M('integral')->where("time <= $endstr")->delete();
				M('fenadd')->where("addtime <= $endstr")->delete();
				M('fenxia')->where("addtime <=$endstr")->delete();
				M('caiji')->where("addtime <= $endstr")->delete();
				M('fs_date')->where("add_time <= $endstr")->delete();
				M('fs_details')->where("date <= $endtimestr")->delete();
				M('kongzhi')->where("addtime <= $endstr")->delete();
				M('message')->where("time <= $endtimestr")->delete();
				M('number')->where("time <= $endstr")->delete();
				M('push_money')->where("time <= $endstr")->delete();
				M('userlog')->where("addtime <= $endstr")->delete();
				$this->success('删除成功!',U('Admin/Index/index'),1);
			
			}
		} else {
			$this->display();
		}
	}



	public function admin_cancel(){
		$id = I('id');
		if($id){
			$order = M('order')->where("id={$id}")->find();
			if ($order['add_points']) {
				$add = $order['add_points']-$order['del_points'];
				$res = M('user')->where("id = {$order['userid']}")->setDec('points',$add);
				$res1 = M('order')->where("id = $id")->setField(array('is_add'=>'1','del_points'=>0,'add_points'=>0,'state'=>0));
			} else{
				$points = $order['del_points'];
				$res = M('user')->where("id = {$order['userid']}")->setInc('points',$points);
				$res1 = M('order')->where("id = $id")->setField(array('is_add'=>'1','del_points'=>0,'add_points'=>0,'state'=>0));
			}
		
			if($res && $res1){
				$this->success('取消成功');
			} else {
				$this->error('取消失败！');
			}
		}
	}
	
	//每日输赢
	public function win_lose(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		$map['state'] = 1;
		$map['is_robot'] = 0;

		$time = I('time');
		$start = strtotime(str_replace("+"," ",I('starttime'))) ? : mktime(0,0,0,date('m'),date('d'),date('Y'));
		$end = strtotime(str_replace("+"," ",I('endtime')))  ? : mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		
		if($time){
			// $start = strtotime($time.'00:00:00');
			// $end = strtotime($time.'23:59:59');
			if ($time == 'today') {
				// if (time() <= strtotime("06:00:00")) {
		  //   		$start=mktime(6,0,0,date('m'),date('d')-1,date('Y'));
				// 	$end=mktime(6,0,0,date('m'),date('d'),date('Y'))-1;
		  //   	}else{
		  //   		$start=mktime(6,0,0,date('m'),date('d'),date('Y'));
				// 	$end=mktime(6,0,0,date('m'),date('d')+1,date('Y'))-1;
		  //   	}
				$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			} elseif($time == 'yestoday') {
				// if (time() <= strtotime("06:00:00")) {
		  //   		$start=mktime(6,0,0,date('m'),date('d')-2,date('Y'));
				// 	$end=mktime(6,0,0,date('m'),date('d')-1,date('Y'))-1;
		  //   	}else{
		  //   		$start=mktime(6,0,0,date('m'),date('d')-1,date('Y'));
				// 	$end=mktime(6,0,0,date('m'),date('d'),date('Y'))-1;
		  //   	}

				$start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
			}elseif($time == 'beforeys') {
				$start=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')-1,date('Y'))-1;
			} 
			elseif($time == 'week') {
				$start=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			}  elseif($time == 'month') {
				$start=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			}
		}

		$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		$order = M('order');
		$list = $order->field("SUM(add_points) AS add_points,SUM(del_points) AS del_points")->where($map)->find();

		$map_yong['time'] = array(array('egt',$start),array('elt',$end),'and');
		$yong = M('user')->field("sum(money) as sum_yong")->where($map_yong)->find();

		$list['yong'] = $yong['sum_yong']?:0;
	
		$this->assign('nickname',$nickname);
		$this->assign('del',$list['del_points']?:0);
		$this->assign('yong',$list['yong']);
		$this->assign('add',$list['add_points']?:0) ;
		$this->assign('ying',$list['del_points']-$list['add_points']-$list['yong']);
		$this->assign('start',date('Y-m-d H:i:s',$start));
		$this->assign('end',date('Y-m-d H:i:s',$end));
		$this->display();
	}
	
	
	//用户每日输赢
	public function user_win_lose(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		$nickname = I('nickname');
		$time = I('time');
		$map = array();
		if($nickname){
			if (is_numeric($nickname)) {
				$map['username'] = array('like',"%$nickname%");
			} else{
				$map['nickname'] = array('like',"%$nickname%");
			}
		}

		$map['state'] = 1;
		$map['is_robot'] = 0;
		$time = I('time');
		$start = strtotime(str_replace("+"," ",I('starttime'))) ? : mktime(0,0,0,date('m'),date('d'),date('Y'));
		$end = strtotime(str_replace("+"," ",I('endtime'))) ? : mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		
		if($time){
			if ($time == 'today') {
				$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			} elseif($time == 'yestoday') {
				$start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
			} elseif($time == 'beforeys') {
				$start=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')-1,date('Y'))-1;
			} elseif($time == 'week') {
				$start=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			}  elseif($time == 'month') {
				$start=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			}
		}

		$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		$order = M('order');

		$count = $order->where($map)->group('userid')->select();
		$page = new \Think\Page(count($count),10);
		$show = $page->show();

		$list = $order->field("userid,nickname,SUM(add_points) AS add_points,SUM(del_points) AS del_points,count(number) as count_xz")->where($map)->group('userid')->limit($page->firstRow.','.$page->listRows)->select();
		// var_dump($list);exit;
		foreach ($list as $key => $value) {
			$list[$key]['ying'] = $value['add_points'] - $value['del_points'];
			$userinfo = M('user')->where("id={$value['userid']}")->field("headimgurl,username,t_id")->find();
			$list[$key]['headimgurl'] = $userinfo['headimgurl'];	
			$list[$key]['username'] = $userinfo['username'];	
			$list[$key]['t_id'] = $userinfo['t_id'];	
			$list[$key]['remark'] = $userinfo['remark'];
			$list[$key]['points'] = $userinfo['points'];

			//总期数
			$dxds_qi = $order->field("count(1) as count_dxds")->where($map)->where("userid = {$value['userid']}")->group('number')->select();
			$list[$key]['z_qi'] = count($dxds_qi);

			//大小单双流水
			$dxds_sum = $order->field("SUM(del_points) AS del_points,count(number) as count_dxds")->where($map)->where("userid = {$value['userid']} and type = 1")->find();

			//大小单双期数
			$dxds_qi = $order->field("count(1) as count_dxds")->where($map)->where("userid = {$value['userid']} and type = 1")->group('number')->select();
			// var_dump($dxds_qi);exit;
			$list[$key]['dxds'] = $dxds_sum['del_points'];
			$list[$key]['count_dxds'] = count($dxds_qi);

			$chdwq_sum = $order->field("SUM(del_points) AS del_points,count(number) as count_dxds")->where($map)->where("userid = {$value['userid']} and type = 2")->find();
			$chdwq_qi = $order->field("count(1) as count_dxds")->where($map)->where("userid = {$value['userid']} and type = 2")->group('number')->select();
			$list[$key]['chdwq'] = $chdwq_sum['del_points'];
			$list[$key]['count_chdwq'] = count($chdwq_qi);
		}
		// var_dump($list);exit;
		
		$this->assign('nickname',$nickname);
		$this->assign('list',$list);
		$this->assign('time',$time);
		$this->assign('show',$show);
		$this->assign('start',date('Y-m-d H:i:s',$start));
		$this->assign('end',date('Y-m-d H:i:s',$end));
		$this->display();
	}
}


?>